﻿using System.ComponentModel.DataAnnotations;
using Silverlight.EntityFramework;
using System;

namespace Silverlight.HierarchicalModel.Model
{
    public class ObjectEntity : Entity<ObjectEntity>
    {
        private IEntity<string> _name;
        private CollectionEntity<PropertyEntity> _properties;
        private CollectionEntity<ObjectEntity> _objects;

        public ObjectEntity()
        {
            _objects = new CollectionEntity<ObjectEntity>();
            _properties = new CollectionEntity<PropertyEntity>();
            _name = Entity<ObjectEntity, string>.Create(new ObjectEntityNameValidationValue("Name", this));
        }

        [Display(Name = "name")]
        public string Name { get { return _name.Object; } set { _name.Object = value; } }

        [Display(AutoGenerateField = false)]
        public CollectionEntity<ObjectEntity> Objects { get { return _objects; } }

        [Display(AutoGenerateField = false)]
        public CollectionEntity<PropertyEntity> Properties { get { return _properties; } }
    }

    public class ObjectEntityNameValidationValue : EntityValidationValue<ObjectEntity, string>
    {
        public ObjectEntityNameValidationValue(string name, ObjectEntity owner) : base(name, owner) { }

        public override bool Validate(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException(Name);
            }
            return true;
        }
    }
}